package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     *分页查询分类
     * @param c
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO c) {
        PageHelper.startPage(c.getPage(),c.getPageSize());
        Page<Category> page = categoryMapper.page(c);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = Category.builder()
                .status(status).id(id).updateTime(LocalDateTime.now()).updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void insert(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .createTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .status(0)
                .build();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.insert(category);

    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.update(category);
    }
    /**
     * 删除分类
     * @param id
     * @return
     */
    @Override
    public void deleteById(Long id) {
        //查询该分类通过id
        Category category = categoryMapper.getById(id);
        //判断是1菜品分类还是2套餐分类
        if(category.getType() == 1){
            //查询菜品是否为空

           if(!dishMapper.getByCateGory(id).isEmpty()){
               throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
           }
        }else{
            //查询套餐是否为空

            if(!setmealMapper.getByCateGory(id).isEmpty()){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
            }
        }
        //删除分类
       categoryMapper.delete(id);
    }
    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        List<Category> list = categoryMapper.list(type);
        return list;
    }

    /**
     * 查询分类列表
     * @param type
     * @return
     */
    @Override
    public List<Category> getList(Integer type) {
       List<Category> list = categoryMapper.getList(type);
        return list;
    }
}
